package com.yupi.yupicturebackend.controller;

import com.yupi.yupicturebackend.api.deepseek.DeepseekApi;
import com.yupi.yupicturebackend.manager.auth.annotation.SaSpaceCheckPermission;
import com.yupi.yupicturebackend.manager.auth.model.SpaceUserPermissionConstant;
import io.swagger.annotations.Api;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/deepseek")
@Api(tags = "DeepSeek接口")
public class DeepSeekController {
   @Resource
   private DeepseekApi deepseekApi;
//    @PostMapping("/chat")
//    public void getResponse(@RequestBody String prompt) throws IOException {
//       deepseekApi.streamChatResponse(prompt);
//    }


   @GetMapping(value = "/stream-chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//   @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
   public SseEmitter streamChat(@RequestParam String locationName) {
      SseEmitter emitter = new SseEmitter(60_000L); // 60秒超时

      // 异步处理流式响应
      CompletableFuture.runAsync(() -> {
         try {
            deepseekApi.streamChatResponse(locationName, emitter);
         } catch (Exception e) {
            emitter.completeWithError(e);
         }
      });

      return emitter;
   }

   @GetMapping(value = "/searchKeywords")
   public List<String> parseImageSearchKeywords(@RequestParam String userQuery) throws IOException {
      return deepseekApi.parseImageSearchKeywords(userQuery);
   }

}
